﻿using Autofac.Core;
using Autofac;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Model.GateWay;
using Ewell.Interface.Common.Model.GateWay.Dto.Dict;
using Ewell.Interface.Common.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Ewell.BizComponent.Service.Server;
using Ewell.BizComponent.Service.IServer;
using Ewell.BizComponent.UI.Reg.Windows;
using Ewell.BizComponent.UI.Charge.Windows;
using Ewell.Interface.Common.Constant;
using Ewell.BizComponent.UI.Core.Model;
using Ewell.BizComponent.UI.Main.Windows;
using Ewell.BizComponent.UI.Main.ViewModel;
using Ewell.BizComponent.UI.Main.Model;
using Ewell.Interface.Common.Model.ProxyRequest;
using MahApps.Metro.Controls.Dialogs;
using Ewell.Interface.Common.Model.GateWay.Dto.Basic;
using Ewell.Interface.Common.Model.GateWay.GateWayUriInfo;
using Ewell.BizComponent.UI.Core.VievModel;
using Ewell.BizComponent.UI.Reg.ViewModel;
using Ewell.BizComponent.UI.Reg.Views;
using System.Diagnostics;
using System.IO.MemoryMappedFiles;
using System.Text;
using Ewell.BizComponent.UI.Reg.Model.Request;
using Ewell.BizComponent.UI.Charge.ViewModel;
using Ewell.BizComponent.Service.Model.Request;
using System.IO;
using Ewell.BizComponent.UI.Charge.Model.Request;
using Ewell.BizComponent.UI.Charge.Model.Response;
using Ewell.BizComponent.UI.OutSetl.Windows;
using Ewell.BizComponent.UI.OutSetl.ViewModel;
using Ewell.BizComponent.UI.Reserve.Model.Request;
using Ewell.BizComponent.UI.Reserve.Model.Response;
using Ewell.BizComponent.UI.Reserve.Windows;
using Ewell.BizComponent.UI.Reserve.ViewModel;

namespace Ewell.BizComponent.UI.Main
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        //DI
        private static ContainerBuilder builder = new ContainerBuilder();
        protected override void OnStartup(StartupEventArgs e)
        {
            
            System.Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
            base.OnStartup(e);
            LogHelper.Init("业务接口", "诊间业务");
            #region 注册服务
            builder.RegisterType<OutRegisterServer>().As<IOutRegisterServer>();
            builder.RegisterType<OutSettlementServer>().As<IOutSettlementServer>();
            builder.RegisterType<InsurServer>().As<IInsurServer>();
            builder.RegisterType<ReadCardServer>().As<IReadCardServer>();
            builder.RegisterType<PaymentServer>().As<IPaymentServer>();
            builder.RegisterType<InvoiceServer>().As<IInvoiceServer>();
            builder.RegisterInstance(DialogCoordinator.Instance).As<IDialogCoordinator>();
            #endregion

            var container = builder.Build();

            Global.Container = container;
            //测试
            Global.RuntimeEnv = "ThirdPart";
            InterfaceClientResult result1 = BizInvoke(e);
            ReturnResponse(e.Args, result1);
            return;
            //主窗体
            string param = string.Join("", e.Args);
            if (string.IsNullOrWhiteSpace(param))
            {//为空按应用启动
                Application.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;
                MainWindow window = new MainWindow();
                LoginWindow loginWindow = new LoginWindow();
                loginWindow.ShowDialog();
                var loginVm = loginWindow.DataContext as LoginVm;
                if (loginVm == null)
                {
                    this.Shutdown();
                    return;
                }
                LoginInfo.loginUser = loginVm.user;
                LoginInfo.menuId = loginVm.menuId;
                window.Show();
            }
            else
            {//不为空为外部调用
                Global.RuntimeEnv = "ThirdPart";
                InterfaceClientResult result = BizInvoke(e);
                ReturnResponse(e.Args, result);
                return;
            }
        }

        private InterfaceClientResult BizInvoke(StartupEventArgs e)
        {
            InterfaceClientResult result = new InterfaceClientResult()
            {
                success = false,
            };
            BizRequest bizRequest = default;
            LoginInfo.loginUser = default;
            ProxyHeader proxyHeader = default;
            try
            {
                bizRequest = GetRequestParam<BizRequest>(e.Args);
                if (bizRequest == null)
                    throw new SpeedException("HIS", "参数为空或格式不正确！");
                LoginInfo.loginUser = JsonConvert.DeserializeObject<User>(bizRequest.authentication);
                if (LoginInfo.loginUser == null)
                    throw new SpeedException("HIS", "用户信息为空！");
                proxyHeader = JsonConvert.DeserializeObject<ProxyHeader>(bizRequest.header);
                if (proxyHeader == null)
                    throw new SpeedException("HIS", "header不能为空！");
            }
            catch (SpeedException ex)
            {
                LogHelper.Debug(ex.ToString());
                result.message = "错误的参数!" + ex.Message;
                return result;
            }
            catch (Exception ex)
            {
                LogHelper.Debug(ex.ToString());
                result.message = "错误的参数!" + ex.Message;
                return result;
            }

            if (proxyHeader.tradeCode == "101")//挂号
            {
                OutRegisterRequest outRegisterRequest = JsonConvert.DeserializeObject<OutRegisterRequest>(bizRequest.domain);
                LoginInfo.menuId = outRegisterRequest.menuId;
                result = BizOutRegister(outRegisterRequest);
            }
            else if (proxyHeader.tradeCode == "201")//收费
            {
                OutChargeRequest outChargeRequest = JsonConvert.DeserializeObject<OutChargeRequest>(bizRequest.domain);
                LoginInfo.menuId = outChargeRequest.menuId;
                result = OutCharge(outChargeRequest);
            }
            else if (proxyHeader.tradeCode == "301")//预约
            {
                OutpReserveRequest outpReserveRequest = JsonConvert.DeserializeObject<OutpReserveRequest>(bizRequest.domain);
                LoginInfo.menuId = outpReserveRequest.menuId;
                result = OutpReserve(outpReserveRequest);
            }

            return result;
        }

        private InterfaceClientResult OutpReserve(OutpReserveRequest outpReserveRequest)
        {
            OutpReserveResponse response = new OutpReserveResponse();
            InterfaceClientResult result = new InterfaceClientResult()
            {
                success = false,
            };
            try
            {
                OutpReserveRequestVm outpReserveRequestVm = outpReserveRequest.CopyProperties<OutpReserveRequestVm>();
                var mainWindow = new EncounterReserveWindow(outpReserveRequestVm);
                mainWindow.ShowDialog();
                result.success = true;
                result.@object = (mainWindow.DataContext as EncounterReserveWindow).encounterReserveVm.outpReserveVm.outpReserveResponse;
            }
            catch (SpeedException ex)
            {
                LogHelper.Debug(ex.ToString());
                this.Shutdown();
                result.message = ex.Message;
                return result;
            }
            catch (Exception ex)
            {
                LogHelper.Debug(ex.ToString());
                this.Shutdown();
                result.message = ex.Message;
                return result;
            }
            return result;
        }

        private InterfaceClientResult OutCharge(OutChargeRequest request)
        {
            OutChargeResponse response=new OutChargeResponse();
            InterfaceClientResult result = new InterfaceClientResult()
            {
                success = false,
            };
            try
            {
                #region 查询患者信息
                PersonDubboService personDubboService = new PersonDubboService();
                PersonPatientQueryRequest patientQueryRequest = new PersonPatientQueryRequest()
                {
                    patientIds = new List<string>() { request.patientId },
                    personInfoTypes = new List<string>() { "2" },
                };
                var gateWayPatList = personDubboService.queryPersonByExample.Post<List<PersonPatientResponse>>(patientQueryRequest).CheckState("诊间挂号");
                #endregion
                if (gateWayPatList.@object == null || gateWayPatList.@object.Count == 0) throw new SpeedException("诊间支付", "未查询到患者信息!");
                PatientVm patient = gateWayPatList.@object.FirstOrDefault().CopyProperties<PatientVm>();
                var mainWindow = new OutSettlementWindow()
                {
                    outSettlementVm = new OutSettlementVm()
                    {
                        outChargeRequest = request,
                        patient = patient,
                    },
                };
                mainWindow.outSettlementVm.mainWindow = mainWindow;
                mainWindow.ShowDialog();
                result.success = true;
                result.@object = (mainWindow.DataContext as OutSettlementWindow).outSettlementVm.outChargeResponse;
            }
            catch (SpeedException ex)
            {
                LogHelper.Debug(ex.ToString());
                this.Shutdown();
                UnlockRequest unlockRequest = new UnlockRequest()
                {
                    patientId = request.patientId,
                };
                OutPatientPayLockDubboService outPatientPayLockDubboService = new OutPatientPayLockDubboService();
                outPatientPayLockDubboService.unlock.Post<object>(unlockRequest);
                result.message = ex.Message;
                return result;
            }
            catch (Exception ex)
            {
                LogHelper.Debug(ex.ToString());
                this.Shutdown();
                UnlockRequest unlockRequest = new UnlockRequest()
                {
                    patientId = request.patientId,
                };
                OutPatientPayLockDubboService outPatientPayLockDubboService = new OutPatientPayLockDubboService();
                outPatientPayLockDubboService.unlock.Post<object>(unlockRequest);
                result.message = ex.Message;
                return result;
            }
            return result;
        }
        /// <summary>
        /// 诊间挂号
        /// </summary>
        /// <param name="outRegisterRequest"></param>
        /// <returns></returns>
        private InterfaceClientResult BizOutRegister(OutRegisterRequest outRegisterRequest)
        {
            InterfaceClientResult result = new InterfaceClientResult()
            {
                success = false,
            };
            try
            {
                //外部调用挂号参数
                OutRegisterRequestVm requestVm = outRegisterRequest.CopyProperties<OutRegisterRequestVm>();

                var mainWindow = new OutRegWindow()
                {
                    outRegVm = new OutRegVm()
                    {
                        bizOutRegisterRequest = requestVm,
                    }
                };
                mainWindow.ShowDialog();
                result.success = true;
                result.@object = (mainWindow.DataContext as OutRegWindow).outRegVm.outRegisterResponse;
            }
            catch (SpeedException ex)
            {
                LogHelper.Debug(ex.ToString());
                this.Shutdown();
                result.message = ex.Message;
                return result;
            }
            catch (Exception ex)
            {
                LogHelper.Debug(ex.ToString());
                this.Shutdown();
                result.message = ex.Message;
                return result;
            }
            return result;
        }

        private string GetRequestParam(string[] args)
        {
            //测试
            return "{\"authentication\":\"{\\\"no\\\":\\\"2615\\\",\\\"code\\\":\\\"2615\\\",\\\"deptCode\\\":\\\"1060000\\\",\\\"staffCode\\\":\\\"2615\\\",\\\"userName\\\":\\\"黄宇凡\\\",\\\"userNameCn\\\":\\\"黄宇凡\\\",\\\"account\\\":\\\"2615\\\",\\\"userId\\\":\\\"2615\\\",\\\"userCode\\\":\\\"2615\\\",\\\"id\\\":null,\\\"loginToken\\\":\\\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiIyNjE1IiwiZXhwIjoxNjk4MTcwNzEyLCJ1c2VySWQiOiIxNDgzMjMzODE3NzYxNTE3NTY4In0.ocbNjBX-MOPOWvRa2OFEfNcQQCNIBtYQo-fvN-kYqAc\\\",\\\"deptName\\\":\\\"神经内科\\\",\\\"isPasswordCompliance\\\":true,\\\"passwordRules\\\":null,\\\"passwordRulesRemark\\\":null,\\\"zone\\\":\\\"002\\\",\\\"zoneName\\\":\\\"山东省滨州医学院附属医院1\\\",\\\"ipAddress\\\":null,\\\"computerName\\\":null,\\\"macAddress\\\":null,\\\"bizSourceCode\\\":\\\"4\\\"}\",\"header\":\"{\\\"tradeCode\\\":\\\"201\\\",\\\"employeeNo\\\":\\\"2615\\\",\\\"employeeName\\\":\\\"黄宇凡\\\",\\\"deptNo\\\":\\\"30010301\\\",\\\"deptName\\\":\\\"门诊收费室\\\",\\\"interfaceId\\\":\\\"1335424906435072000\\\",\\\"alreadySelectDataList\\\":[],\\\"zone\\\":\\\"33331\\\",\\\"cardInfo\\\":{}}\",\"domain\":\"{\r\n\t\\\"bizSourceCode\\\": \\\"14\\\",\r\n\t\\\"menuId\\\": \\\"1444450153871544320\\\",\r\n\t\\\"patientName\\\": \\\"宋学栋\\\",\r\n\t\\\"patientId\\\": \\\"202183\\\",\r\n\t\\\"outEncounterId\\\": \\\"10110957\\\",\r\n\t\\\"presList\\\": [\r\n\t\t{\r\n\t\t\t\\\"zoneCode\\\": \\\"002\\\",\r\n\t\t\t\\\"executUnitCategCoding\\\": \\\"1\\\",\r\n\t\t\t\\\"orderDetailList\\\": [\r\n\t\t\t\t{\r\n\t\t\t\t\t\\\"chargeDetailList\\\": [\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\\\"retailAmount\\\": \\\"46\\\",\r\n\t\t\t\t\t\t\t\\\"retailPrice\\\": \\\"46\\\",\r\n\t\t\t\t\t\t\t\\\"retailQuantity\\\": \\\"1\\\",\r\n\t\t\t\t\t\t\t\\\"babyAddFlag\\\": \\\"0\\\",\r\n\t\t\t\t\t\t\t\\\"herbUsedWay\\\": \\\"\\\",\r\n\t\t\t\t\t\t\t\\\"needUploadFlag\\\": \\\"1\\\",\r\n\t\t\t\t\t\t\t\\\"medicareCheckStatus\\\": \\\"0\\\",\r\n\t\t\t\t\t\t\t\\\"retailUnitDesc\\\": \\\"瓶\\\",\r\n\t\t\t\t\t\t\t\\\"drugFlag\\\": \\\"1\\\",\r\n\t\t\t\t\t\t\t\\\"commodityName\\\": \\\"华法林钠片\\\",\r\n\t\t\t\t\t\t\t\\\"commodityCode\\\": \\\"65362\\\",\r\n\t\t\t\t\t\t\t\\\"commodityFid\\\": \\\"65362\\\",\r\n\t\t\t\t\t\t\t\\\"chargeDetailFid\\\": \\\"1487138416423653376\\\",\r\n\t\t\t\t\t\t\t\\\"retailUnitFactor\\\": 100,\r\n\t\t\t\t\t\t\t\\\"insurMinUploadFlag\\\": null\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t],\r\n\t\t\t\t\t\\\"retailQuantity\\\": \\\"1\\\",\r\n\t\t\t\t\t\\\"orderItemName\\\": \\\"华法林钠片\\\",\r\n\t\t\t\t\t\\\"orderItemCode\\\": \\\"65362\\\",\r\n\t\t\t\t\t\\\"orderDetailFid\\\": \\\"2250132\\\"\r\n\t\t\t\t},\r\n\t\t\t\t{\r\n\t\t\t\t\t\\\"chargeDetailList\\\": [\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\\\"retailAmount\\\": \\\"45.3\\\",\r\n\t\t\t\t\t\t\t\\\"retailPrice\\\": \\\"4.53\\\",\r\n\t\t\t\t\t\t\t\\\"retailQuantity\\\": \\\"10\\\",\r\n\t\t\t\t\t\t\t\\\"babyAddFlag\\\": \\\"0\\\",\r\n\t\t\t\t\t\t\t\\\"herbUsedWay\\\": \\\"\\\",\r\n\t\t\t\t\t\t\t\\\"needUploadFlag\\\": \\\"1\\\",\r\n\t\t\t\t\t\t\t\\\"medicareCheckStatus\\\": \\\"0\\\",\r\n\t\t\t\t\t\t\t\\\"retailUnitDesc\\\": \\\"袋\\\",\r\n\t\t\t\t\t\t\t\\\"drugFlag\\\": \\\"1\\\",\r\n\t\t\t\t\t\t\t\\\"commodityName\\\": \\\"富马酸比索洛尔片\\\",\r\n\t\t\t\t\t\t\t\\\"commodityCode\\\": \\\"70864\\\",\r\n\t\t\t\t\t\t\t\\\"commodityFid\\\": \\\"70864\\\",\r\n\t\t\t\t\t\t\t\\\"chargeDetailFid\\\": \\\"1487138416423653377\\\",\r\n\t\t\t\t\t\t\t\\\"retailUnitFactor\\\": 100,\r\n\t\t\t\t\t\t\t\\\"insurMinUploadFlag\\\": null\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t],\r\n\t\t\t\t\t\\\"retailQuantity\\\": \\\"10\\\",\r\n\t\t\t\t\t\\\"orderItemName\\\": \\\"富马酸比索洛尔片\\\",\r\n\t\t\t\t\t\\\"orderItemCode\\\": \\\"70864\\\",\r\n\t\t\t\t\t\\\"orderDetailFid\\\": \\\"2250133\\\"\r\n\t\t\t\t},\r\n\t\t\t\t{\r\n\t\t\t\t\t\\\"chargeDetailList\\\": [\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\\\"retailAmount\\\": \\\"495\\\",\r\n\t\t\t\t\t\t\t\\\"retailPrice\\\": \\\"495\\\",\r\n\t\t\t\t\t\t\t\\\"retailQuantity\\\": \\\"1\\\",\r\n\t\t\t\t\t\t\t\\\"babyAddFlag\\\": \\\"0\\\",\r\n\t\t\t\t\t\t\t\\\"herbUsedWay\\\": \\\"\\\",\r\n\t\t\t\t\t\t\t\\\"needUploadFlag\\\": \\\"1\\\",\r\n\t\t\t\t\t\t\t\\\"medicareCheckStatus\\\": \\\"0\\\",\r\n\t\t\t\t\t\t\t\\\"retailUnitDesc\\\": \\\"瓶\\\",\r\n\t\t\t\t\t\t\t\\\"drugFlag\\\": \\\"1\\\",\r\n\t\t\t\t\t\t\t\\\"commodityName\\\": \\\"托伐普坦片\\\",\r\n\t\t\t\t\t\t\t\\\"commodityCode\\\": \\\"70599\\\",\r\n\t\t\t\t\t\t\t\\\"commodityFid\\\": \\\"70599\\\",\r\n\t\t\t\t\t\t\t\\\"chargeDetailFid\\\": \\\"1487138416423653378\\\",\r\n\t\t\t\t\t\t\t\\\"retailUnitFactor\\\": 100,\r\n\t\t\t\t\t\t\t\\\"insurMinUploadFlag\\\": null\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t],\r\n\t\t\t\t\t\\\"retailQuantity\\\": \\\"1\\\",\r\n\t\t\t\t\t\\\"orderItemName\\\": \\\"托伐普坦片\\\",\r\n\t\t\t\t\t\\\"orderItemCode\\\": \\\"70599\\\",\r\n\t\t\t\t\t\\\"orderDetailFid\\\": \\\"2250132\\\"\r\n\t\t\t\t},\r\n\t\t\t\t{\r\n\t\t\t\t\t\\\"chargeDetailList\\\": [\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\\\"retailAmount\\\": \\\"1065\\\",\r\n\t\t\t\t\t\t\t\\\"retailPrice\\\": \\\"1065\\\",\r\n\t\t\t\t\t\t\t\\\"retailQuantity\\\": \\\"10\\\",\r\n\t\t\t\t\t\t\t\\\"babyAddFlag\\\": \\\"0\\\",\r\n\t\t\t\t\t\t\t\\\"herbUsedWay\\\": \\\"\\\",\r\n\t\t\t\t\t\t\t\\\"needUploadFlag\\\": \\\"1\\\",\r\n\t\t\t\t\t\t\t\\\"medicareCheckStatus\\\": \\\"0\\\",\r\n\t\t\t\t\t\t\t\\\"retailUnitDesc\\\": \\\"袋\\\",\r\n\t\t\t\t\t\t\t\\\"drugFlag\\\": \\\"1\\\",\r\n\t\t\t\t\t\t\t\\\"commodityName\\\": \\\"注射用醋酸卡泊芬净(恒瑞）\\\",\r\n\t\t\t\t\t\t\t\\\"commodityCode\\\": \\\"70602\\\",\r\n\t\t\t\t\t\t\t\\\"commodityFid\\\": \\\"70602\\\",\r\n\t\t\t\t\t\t\t\\\"chargeDetailFid\\\": \\\"1487138416423653379\\\",\r\n\t\t\t\t\t\t\t\\\"retailUnitFactor\\\": 100,\r\n\t\t\t\t\t\t\t\\\"insurMinUploadFlag\\\": null\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t],\r\n\t\t\t\t\t\\\"retailQuantity\\\": \\\"10\\\",\r\n\t\t\t\t\t\\\"orderItemName\\\": \\\"注射用醋酸卡泊芬净(恒瑞）\\\",\r\n\t\t\t\t\t\\\"orderItemCode\\\": \\\"70602\\\",\r\n\t\t\t\t\t\\\"orderDetailFid\\\": \\\"2250133\\\"\r\n\t\t\t\t}\r\n\t\t\t],\r\n\t\t\t\\\"presedAt\\\": \\\"2023-11-07T00:14:33.000Z\\\",\r\n\t\t\t\\\"retailAmount\\\": \\\"91.30\\\",\r\n\t\t\t\\\"bizSourceCoding\\\": \\\"14\\\",\r\n\t\t\t\\\"priortyFlag\\\": \\\"0\\\",\r\n\t\t\t\\\"herbTieNumber\\\": 0,\r\n\t\t\t\\\"executdByDeptCode\\\": \\\"1050000\\\",\r\n\t\t\t\\\"presedByDeptCode\\\": \\\"1050000\\\",\r\n\t\t\t\\\"presedByUserCode\\\": \\\"2615\\\",\r\n\t\t\t\\\"medicareTyeCoding\\\": \\\"-1\\\",\r\n\t\t\t\\\"presTypeCoding\\\": \\\"1\\\",\r\n\t\t\t\\\"presNo\\\": \\\"1850751\\\",\r\n\t\t\t\\\"elecRequisitionNo\\\": null,\r\n\t\t\t\\\"presFid\\\": \\\"1850751\\\"\r\n\t\t}\r\n\t]\r\n}\"}";
            //return "{\"authentication\":\"{\\\"no\\\":\\\"2615\\\",\\\"code\\\":\\\"2615\\\",\\\"deptCode\\\":\\\"1060000\\\",\\\"staffCode\\\":\\\"2615\\\",\\\"userName\\\":\\\"黄宇凡\\\",\\\"userNameCn\\\":\\\"黄宇凡\\\",\\\"account\\\":\\\"2615\\\",\\\"userId\\\":\\\"2615\\\",\\\"userCode\\\":\\\"2615\\\",\\\"id\\\":null,\\\"loginToken\\\":\\\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpbk5hbWUiOiIyNjE1IiwiZXhwIjoxNjk4MTcwNzEyLCJ1c2VySWQiOiIxNDgzMjMzODE3NzYxNTE3NTY4In0.ocbNjBX-MOPOWvRa2OFEfNcQQCNIBtYQo-fvN-kYqAc\\\",\\\"deptName\\\":\\\"神经内科\\\",\\\"isPasswordCompliance\\\":true,\\\"passwordRules\\\":null,\\\"passwordRulesRemark\\\":null,\\\"zone\\\":\\\"33331\\\",\\\"zoneName\\\":\\\"织金县人民医院院区\\\",\\\"ipAddress\\\":null,\\\"computerName\\\":null,\\\"macAddress\\\":null,\\\"bizSourceCode\\\":\\\"4\\\"}\",\"header\":\"{\\\"tradeCode\\\":\\\"101\\\",\\\"employeeNo\\\":\\\"2615\\\",\\\"employeeName\\\":\\\"黄宇凡\\\",\\\"deptNo\\\":\\\"30010301\\\",\\\"deptName\\\":\\\"门诊收费室\\\",\\\"interfaceId\\\":\\\"1335424906435072000\\\",\\\"alreadySelectDataList\\\":[],\\\"zone\\\":\\\"33331\\\",\\\"cardInfo\\\":{}}\",\"domain\":\"{\\r\\n\\t\\\"menuId\\\": \\\"1444450153871544320\\\",\\r\\n\\t\\\"bizSourceCode\\\": \\\"4\\\",\\r\\n\\t\\\"patientId\\\": \\\"202183\\\",\\r\\n\\t\\\"patientName\\\": \\\"宋学栋\\\",\\r\\n\\t\\\"docFid\\\": null,\\r\\n\\t\\\"docCode\\\": \\\"2615\\\",\\r\\n\\t\\\"docName\\\": \\\"黄宇凡\\\",\\r\\n\\t\\\"deptFid\\\": null,\\r\\n\\t\\\"deptCode\\\": 1060000,\\r\\n\\t\\\"deptName\\\": \\\"耳鼻科\\\"\\r\\n}\"}";
            string result = string.Empty;
            long capacity = default(long);
            if (args.Length != 2)
                throw new SpeedException("HIS", "参数数量不正确!");
            try
            {
                capacity = Convert.ToInt64(args[1]);
            }
            catch (Exception ex)
            {
                throw new SpeedException("HIS", "参数格式不正确!" + ex.Message);
            }
            byte[] clearBuffer = new byte[capacity];

            using (MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen(args[0], capacity))
            using (MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor())
            {

                byte[] resultBuffer = new byte[capacity];
                accessor.ReadArray(0, resultBuffer, 0, (int)capacity);


                result = Encoding.UTF8.GetString(resultBuffer).Trim('\0');
                LogHelper.Debug($"请求参数:{result}");
                accessor.WriteArray(0, clearBuffer, 0, clearBuffer.Length);
            }
            return result;
        }

        private T GetRequestParam<T>(string[] args)
        {
            return JsonConvert.DeserializeObject<T>(GetRequestParam(args));
        }

        private void ReturnResponse<T>(string[] args, T response)
        {
            //测试
            return;
            long capacity = default(long);
            if (args.Length != 2)
                throw new SpeedException("HIS", "参数数量不正确!");
            try
            {
                capacity = Convert.ToInt64(args[1]);
            }
            catch (Exception ex)
            {
                throw new SpeedException("HIS", "参数格式不正确!" + ex.Message);
            }
            byte[] clearBuffer = new byte[capacity];
            MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen(args[0], capacity);
            MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
            accessor.WriteArray(0, clearBuffer, 0, clearBuffer.Length);

            var resultStr = JsonConvert.SerializeObject(response);
            LogHelper.Debug($"返回参数:{resultStr}");
            var bytes = Encoding.UTF8.GetBytes(resultStr);
            accessor.WriteArray(0, bytes, 0, bytes.Length);
        }
    }
}
